<template>
  <el-container>
    <el-aside width="200px">
      <my-aside/>
    </el-aside>
    <el-container>
      <el-header>
        <div>
          <span> vueAdmin后台管理系统</span>
          <div v-if="user" style="float: right;display: flex;align-items: center">
            <el-avatar style="margin-right: 15px" :src="user.image?user.image:'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'"></el-avatar>
            <el-dropdown trigger="click">
              <span class="el-dropdown-link">
                {{ user.nickname }}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="$router.push('/home/person')">个人中心</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <el-link target="_blank" href="http://www.baidu.com" style="margin: 0 15px">百度</el-link>
            <el-link target="_blank" href="http://absolutecold.top">绝冷</el-link>
          </div>
        </div>
      </el-header>
      <el-main>
        <el-tabs v-model="tabsValue" type="card" closable @tab-remove="tabRemove">
          <el-tab-pane v-for="(item,index) in editableTabs" :key="item.name" :label="item.title" :name="item.name">
            <div style="padding: 15px">
              <router-view></router-view>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import myAside from "@/components/Aside.vue"

export default {
  name: "Home",
  data() {
    return {
      user: null,
    }
  },
  computed: {
    editableTabs:{
      get(){
        return this.$store.state.menu.tabs
      }
    },
    tabsValue:{
      get(){
        return this.$store.state.menu.tabsValue
      },
      set(val){
        this.$store.commit("setTabsValue",val)
        this.$router.push(val).catch(e =>{})
      }
    }
  },
  methods: {
    getUser() {
      this.$post("/getUser").then(res => {
        console.log(res)
        if (res.data.code == 200) this.user = res.data.data
      })
    },
    logout() {
      this.$post("/logout").then(res => {
        if (res.code == 200) {
          window.localStorage.clear()
          window.sessionStorage.clear()
          this.$store.commit("reset")
          this.$nextTick(=>{
            this.$router.push('/login')
          })
        }
      })
    },
    tabRemove(target){
      this.$store.commit("changeTabs",{target,flag:this.$route.path == target})
    }
  }
  ,
  created() {
    this.getUser()
  }
  ,
  components: {
    myAside
  }
}
</script>

<style scoped>
section {
  padding: 0;
}

.el-container {
  height: 100%;
}

.el-header {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
  overflow: hidden;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  padding: 0;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
